package com.nowcoder.topic.dfs.simple;

/**
 * NC315 相同的二叉树
 * @author d3y1
 */
public class NC315{
    private boolean result = true;

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 程序入口
     *
     * @param root1 TreeNode类
     * @param root2 TreeNode类
     * @return bool布尔型
     */
    public boolean isSameTree (TreeNode root1, TreeNode root2) {
        if(root1==null && root2==null){
            return true;
        }else if(root1==null && root2!=null){
            return false;
        }else if(root1!=null && root2==null){
            return false;
        }else{
            if(root1.val != root2.val){
                return false;
            }
        }

        return isSameTree(root1.left, root2.left) && isSameTree(root1.right, root2.right);
    }

    public boolean isSameTree1 (TreeNode root1, TreeNode root2) {
        preorder(root1, root2);

        return result;
    }

    /**
     * 递归: 前序遍历
     * @param root1
     * @param root2
     */
    private void preorder(TreeNode root1, TreeNode root2){
        if(root1==null && root2==null){
            return;
        }else if(root1==null && root2!=null){
            result = false;
            return;
        }else if(root1!=null && root2==null){
            result = false;
            return;
        }else{
            if(root1.val != root2.val){
                result = false;
                return;
            }
        }

        preorder(root1.left, root2.left);
        preorder(root1.right, root2.right);
    }

    private class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }
}